require('babel-polyfill')
import Koa from 'koa'
import parse from 'koa-bodyparser'
const app = new Koa()

var cors = require('./koa-cors');
app.use(cors);
app.use(parse())

import uuid from 'uuid/v1'
import redis from './redis'
app.use(async (ctx, next) => {
  if (ctx.path === '/redis/set' && ctx.req.method === 'POST') {
    // console.log(',ctx.req url=====>',ctx.req.url);
    // console.log(',ctx.req methoid=====>',ctx.req.method);
    var time = ctx.request && ctx.request.query && ctx.request.query.time || 6
    var key = ctx.request.query && ctx.request.query.key
    if (!key) {
      return false
    }
    var result = await redis.set(key, JSON.stringify(ctx.request.body), { time })
    ctx.body = { result }
    return

  }
  if (ctx.path === '/redis/get' && ctx.req.method === 'GET') {
    var key = ctx.request.query && ctx.request.query.key
    var result = await redis.get(key)
    ctx.body = { result }
    return
  }
  next()
})

app.listen(9013)


console.log(',====>http://localhost:9013');